package client;



import msg.*;
import java.util.*;
import java.io.*;
import java.net.*;
//import jkmessenger.*;
/**
 *
 * @author karol
 */

public class Connection {
	int port;
	InetAddress adres;
        String ip =  "213.134.176.47";

	public Connection(){}
	public Connection(int p){
		port = p;
		try{
			adres = InetAddress.getByName(ip);
		}
		catch(UnknownHostException e){
			System.err.println(e);
		}
	}
	public int register(MsgRegistration ms){
		
		try{
			Socket s = new Socket(adres, port);
			ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
			out.writeObject(ms);
			out.flush();
		        
			DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
			int id = in.readInt();

			System.out.println("Otrzymany id: "+id);
		
			return id;//zwarcam id do dalszych działan
		}
		catch(IOException e){
			System.out.println("Podczas proby nawiazania polaczenia wystapil blad: " + e);
		}	
		return 0;	    
	}

        public MsgHistoryBack history(MsgHistory m){

            try{
                Socket s = new Socket(adres, port);
                ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
                out.writeObject(m);
                out.flush();
                ObjectInputStream in =new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
                Object ob = in.readObject();
                out.close();
                s.close();
                if(ob == null) {
                    System.out.println("Null");
                }
                else if (ob instanceof MsgHistoryBack) {
                    MsgHistoryBack h = (MsgHistoryBack) ob;
           //         System.out.println("Historia zawiera "+h.getSenders().get(0)+" wpisów");
                    return h;
                }

            }
            catch(IOException e){
                    System.out.println("Podczas proby nawiazania polaczenia wystapil blad" + e);
            }
            catch(ClassNotFoundException e) { System.out.println(e); }

            return null;
        }

    public MsgContent receiveMsg(){
        try{
            ServerSocket ss = new ServerSocket(2001);
            Socket s;
            System.out.println("Czekam na wiadomosci");
            s = ss.accept();
            //InetAddress ia = s.getLocalAddress();
            ObjectInputStream in =new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
            Object ob = in.readObject();
            if(ob == null) {
                System.out.println("Null");
            }
            else if (ob instanceof MsgContent) {
                MsgContent m = (MsgContent)ob;

                DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
                out.writeInt(1);//everything fine
                out.flush();
                s.close();
                ss.close();
                return m;

            }
            else{
                throw new ClassNotFoundException("oczekiwano rejestracji - obiekt klasy "+ob);
            }
            return null;
        }
        catch(IOException e){
            System.err.println(e);
        }
        catch(ClassNotFoundException e) { System.out.println(e); }
        return null;
    }

        public void sendMsg(MsgContent m){
            try{
                Socket s = new Socket(adres, port);
                ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
                out.writeObject(m);
                out.flush();

                DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
                int i = in.readInt();//everything fine
                if(i == 1)
                    System.out.println("Jest git");
                out.flush();
                s.close();
            }
            catch(IOException e){
                    System.out.println("Podczas proby nawiazania polaczenia wystapil blad" + e);
            }
        }

        public int status(MsgStatus m){
            int i =0;
            try{
                Socket s = new Socket(adres, port);
                ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
                out.writeObject(m);
                out.flush();

                DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
                i = in.readInt();
                if(i == 0)
                    System.out.println("Wszytsko git");


            }
            catch(IOException e){
                    System.out.println("Podczas proby nawiazania polaczenia wystapil blad" + e);
            }


            return i;
        }

}


